﻿#include "api_client.h"

Client::Client(QObject *parent) : QObject(parent), jwt_token("")
{
    this->uid = 0;

}

void Client::client_modify_info(QJsonObject &data) {

}

void Client::client_modify_info_finished() {
    
}

void Client::get_client_info(QString token, int uid) {
    this->jwt_token = token;
    this->uid = uid;

    qDebug() << this->jwt_token << ' '<< this->uid;

    QString url = "/client/get/client/info/";
    QString jwt_token = token;
    QJsonObject data;
    data["uid"] = uid;
    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &Client::get_client_info_finished);
}

void Client::get_client_info_finished() {
    QJsonObject tmp = this->request->get_return_data().toObject();
//    qDebug() << "----------client-info----------" << this->request->get_return_data() << endl;
    this->id = tmp["id"].toInt();
    this->name = tmp["name"].toString();
    this->phone = tmp["phone"].toString();
    this->prefer = tmp["prefer"].toInt();
    this->sex = tmp["sex"].toString();

    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &Client::get_client_info_finished);
}

void Client::get_all_food() {
    QString url = "/food/get/all/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["uid"] = uid;
    this->food_request = new HttpPostRequest();
    this->food_request->post(url, jwt_token, data);

    QObject::connect(this->food_request, &HttpPostRequest::post_finished, this, &Client::get_all_food_finished);
}
void Client::get_all_food_finished() {
    this->food_list = this->food_request->get_return_data().toArray();
    emit this->handle_get_all_food_finished();
    QObject::disconnect(this->food_request, &HttpPostRequest::post_finished, this, &Client::get_all_food_finished);
}

void Client::get_all_order_by_client_id(){
    QString url = "/order/get/order/by/client/id/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["uid"] = uid;
    data["client_id"] = this->id;
    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &Client::get_all_order_by_client_id_finished);

}
void Client::get_all_order_by_client_id_finished(){
    QJsonArray tmp = this->request->get_return_data().toArray();
    this->order_list = tmp;
    emit this->get_all_order_by_client_id_signal();
    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &Client::get_all_order_by_client_id_finished);
}

void Client::get_all_food_style() {
    QString url = "/food/get/all/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["uid"] = uid;
    this->request = new HttpPostRequest();
    this->request->post(url, jwt_token, data);

    QObject::connect(this->request, &HttpPostRequest::post_finished, this, &Client::get_all_food_style_finished);
}
void Client::get_all_food_style_finished() {
    QJsonObject tmp = this->request->get_return_data().toObject();

    QObject::disconnect(this->request, &HttpPostRequest::post_finished, this, &Client::get_all_food_style_finished);
}

void Client::send_order(QJsonArray orders) {
    QString url = "/order/add/order/and/items/";
    QString jwt_token = this->jwt_token;
    QJsonObject data;
    data["food_list"] = orders;
    data["client_id"] = this->id;
    data["client_num"] = 1;
    data["table_id"] = 3;
    data["is_predetermine"] = "0";

    this->order_request = new HttpPostRequest();
    this->order_request->post(url, jwt_token, data);

    QObject::connect(this->order_request, &HttpPostRequest::post_finished, this, &Client::send_order_finished);
}
void Client::send_order_finished() {
    double tmp = this->order_request->get_return_code();

    emit this->send_order_finished_signal();
    QObject::disconnect(this->order_request, &HttpPostRequest::post_finished, this, &Client::send_order_finished);
}

